package com.lam.schedule.entity;

import java.io.Serializable;
import java.util.Date;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Null;
import javax.validation.constraints.Size;

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.annotation.write.style.ColumnWidth;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.lam.common.base.domain.BaseEntity;
import com.lam.common.components.dict.annotation.Dict;
import com.lam.common.components.easyexcel.converter.DictConverter;
import com.lam.common.utils.DateUtil;
import com.lam.common.utils.JsonUtil;
import com.lam.common.utils.StringUtils;
import com.lam.common.validate.annotation.DictCheck;
import com.lam.common.validate.annotation.DictValid;
import com.lam.common.validate.group.Add;

/**
 * 定时调度对象 schedule_job
 * 
 * @author admin
 * @date 2022-07-18
 */
@DictCheck
@ColumnWidth(30)
@TableName("schedule_job")
public class ScheduleJob implements BaseEntity, Serializable, QrtzTriggerState{

	private static final long serialVersionUID = 1L;

	/** 主键 */
    @ExcelIgnore
    @Null(groups = Add.class, message="id必须为空")
    @TableId(type = IdType.AUTO)
    private String id;
    
    /** 任务名称 */
    @ExcelProperty("任务名称")
    @NotBlank(message = "任务名称不能为空")
    @Size(max = 100, message = "任务名称长度不能超过100个字符")
    private String jobName;
    
    /** 任务分组 */
    @ExcelProperty("任务分组")
    @NotNull(message = "任务分组不能为空")
    @Size(max = 100, message = "任务分组长度不能超过100个字符")
    @DictValid(message = "无效的任务分组")
    @Dict(dictCode = "job_group")
    private String jobGroup;
    
    /** 状态（1正常 0暂停） */
    @ExcelProperty(value = "状态", converter = DictConverter.class)
    @NotNull(message = "状态不能为空")
    @DictValid(message = "无效的状态")
    @Dict(dictCode = "job_status")
    private Integer status;
    
    /**
     * 调度状态
     * 
	 * NONE：Trigger已经完成，且不会在执行，或者找不到该触发器，或者Trigger已经被删除 
	 * NORMAL:正常
	 * PAUSED：暂停 
	 * COMPLETE：触发完成，但是任务可能还正在执行中
	 * BLOCKED：线程阻塞 
	 * ERROR：出现错误
	 * RUNNING:正在执行
	 */
    @ExcelIgnore
    @TableField(exist = false)
    @Dict(dictCode = "schedule_status")
    private String scheduleStatus;
    
    /** con表达式 */
    @ExcelProperty("con表达式")
    @NotBlank(message = "con表达式不能为空")
    @Size(max = 50, message = "con表达式长度不能超过50个字符")
    private String cronExpression;
    
    /** 表达式描述 */
    @ExcelProperty("表达式描述")
    @NotBlank(message = "表达式描述不能为空")
    @Size(max = 100, message = "表达式描述长度不能超过100个字符")
    private String cronExpDesc;
    
    /** 执行类 */
    @ExcelProperty("执行类")
    @NotBlank(message = "执行类不能为空")
    @Size(max = 100, message = "执行类长度不能超过100个字符")
    private String exeClass;
    
    /** 执行方法 */
    @ExcelProperty("执行方法")
    @NotBlank(message = "执行方法不能为空")
    @Size(max = 100, message = "执行方法长度不能超过100个字符")
    private String exeMethod;
    
    /** 执行参数 */
    @ExcelProperty("执行参数")
    @NotBlank(message = "执行参数不能为空")
    @Size(max = 1000, message = "执行参数长度不能超过1000个字符")
    private String exeParam;
    
    /** 并发类型(1-允许并发 0-串行执行) */
    @ExcelProperty(value = "并发类型", converter = DictConverter.class)
    @NotNull(message = "并发类型不能为空")
    @DictValid(message = "无效的并发类型")
    @Dict(dictCode = "concurrent_type")
    private Integer concurrentType;
    
    /** 执行错误策略（1-立即执行 2-执行一次 3-放弃执行） */
    @ExcelProperty(value = "执行错误策略", converter = DictConverter.class)
    @DictValid(message = "无效的执行错误策略")
    @Dict(dictCode = "misfire_policy")
    private Integer misfirePolicy;
    
    /** 记录调度日志(1-是, 0-否) */
    @ExcelProperty(value = "记录日志", converter = DictConverter.class)
    @DictValid(message = "记录日志值无效")
    @Dict(dictCode = "yn10")
    private Integer collectLog;
    
    /** 创建人 */
    @ExcelIgnore
    private String createBy;
    
    /** 创建时间 */
    @ExcelIgnore
    @JsonFormat(pattern = DateUtil.PATTERN_DEFAULT)
    private Date createTime;
    
    /** 修改人 */
    @ExcelProperty("更新者")
    private String updateBy;
    
    /** 修改时间 */
    @ExcelProperty("修改时间")
    @JsonFormat(pattern = DateUtil.PATTERN_DEFAULT)
    private Date updateTime;
    
    /** 备注信息 */
    @ExcelProperty("备注信息")
    @Size(max = 500, message = "备注信息长度不能超过500个字符")
    private String remark;
    
    @Override
	public String getJobKey() {
//    	return getJobGroup() + '.' + jobNameWithId();
		return id;
	}

	@Override
	public String getState() {
		return scheduleStatus;
	}
    
//    public void jobNameWithId(String jobName){
//		String[] ss = StringUtils.split(jobName, "<=>");
//        this.jobName = ss[0]; 
//        if(ss.length > 1){
//        	this.id = ss[1];
//        }
//    }  
	
//	public String jobNameWithId(){
//    	return this.jobName + "<=>" + this.id;  
//    }
	
	@Override
	public String toString() {
		return JsonUtil.toJsonString(this);
	}

	public void setId(String id) {
        this.id = id;
    }

    public String getId() {
        return id;
    } 

    public void setJobName(String jobName) {
        this.jobName = jobName;
    }

    public String getJobName() {
        return jobName;
    }    
    
    public void setJobGroup(String jobGroup) {
        this.jobGroup = jobGroup;
    }

    public String getJobGroup() {
        return jobGroup;
    }    
    
    public void setStatus(Integer status) {
        this.status = status;
    }

    public Integer getStatus() {
        return status;
    }    
    
    public void setCronExpression(String cronExpression) {
        this.cronExpression = cronExpression;
    }

    public String getCronExpression() {
        return cronExpression;
    }    
    
    public void setCronExpDesc(String cronExpDesc) {
        this.cronExpDesc = cronExpDesc;
    }

    public String getCronExpDesc() {
        return cronExpDesc;
    }    
    
    public void setExeClass(String exeClass) {
        this.exeClass = exeClass;
    }

    public String getExeClass() {
        return exeClass;
    }    
    
    public void setExeMethod(String exeMethod) {
        this.exeMethod = exeMethod;
    }

    public String getExeMethod() {
        return exeMethod;
    }    
    
    public void setExeParam(String exeParam) {
        this.exeParam = exeParam;
    }

    public String getExeParam() {
        return exeParam;
    }    
    
    public void setConcurrentType(Integer concurrentType) {
        this.concurrentType = concurrentType;
    }

    public Integer getConcurrentType() {
        return concurrentType;
    }    
    
    public void setMisfirePolicy(Integer misfirePolicy) {
        this.misfirePolicy = misfirePolicy;
    }

    public Integer getMisfirePolicy() {
        return misfirePolicy;
    }    
    
    public void setCreateBy(String createBy) {
        this.createBy = createBy;
    }

    public String getCreateBy() {
        return createBy;
    }    
    
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getCreateTime() {
        return createTime;
    }    
    
    public void setUpdateBy(String updateBy) {
        this.updateBy = updateBy;
    }

    public String getUpdateBy() {
        return updateBy;
    }    
    
    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public Date getUpdateTime() {
        return updateTime;
    }    
    
    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getRemark() {
        return remark;
    }

	public String getScheduleStatus() {
		return scheduleStatus;
	}

	public void setScheduleStatus(String scheduleStatus) {
		this.scheduleStatus = scheduleStatus;
	}
	public void setScheduleStatusAdd(String scheduleStatus) {
		if(StringUtils.isBlank(this.scheduleStatus)) {
			this.scheduleStatus = scheduleStatus;
		}else {
			this.scheduleStatus += ","+scheduleStatus;
		}
	}

	public Integer getCollectLog() {
		return collectLog;
	}

	public void setCollectLog(Integer collectLog) {
		this.collectLog = collectLog;
	}
	


}
